var brandList, pbcList;

$(function () {

	   $("#search_reset").click(function(){
	  		 $("input").val("");
	         $("select").val("");
			 
	    });
   

    //生成二维码
    $("[name='btn_new_save_qrcode']").click(function () {
        var form = window.form_new;
        var qcname = form.qcname.value;
        var type = form.type.value;
        var nubs = form.nubs.value;
        if (!qcname) {
            bootbox.balert("二维码批次输入有误！");
            return;
        }
        
        if (!nubs || isNaN(nubs)) {
            bootbox.balert("数量输入有误！");
            return;
        }
        
        if(nubs>1000000 || nubs <=0){
        	 bootbox.balert("注意：一次最多生成100万瓶码/盒码！");
             return;
        }
     
    	jQuery.ajax({
            type:'post',
            url:'/qrcode/add',
            data:{'qcname':qcname,'type':type,'nubs':nubs},
            dataType:'json',
            cache:false,
            success:function(data){
            	var r = data.data;
            	if(r==-1){
            		tipper.danger("操作失败:二维码批次重复");
            	}else{
            		tipper.success("操作成功！");
            		window.location.href=window.location.href; 
            		window.location.reload; 
            	}
            },
            error:function (data, status) {
                // tipper.danger(data.responseJSON.msg);
                bootbox.alert({
                    message: data.responseJSON.msg,
                    size: "small",
                    title: "<i class='fa fa-info-circle text-yellow'></i> 提示"
                });
            }
       });
        
    	//form.qcname.value = "";
    	form.nubs.value = "";
    	
        
     
    });
    
  //下载
    $("[name='btn_down']").click(function () {
    	  var type=$(this).data("type");
    	  var history=$(this).data("history");
    	  console.log(history);
    	  if(history==0){
    		  var id = $(this).data("id");
              var qcname = $(this).data("qcname");
    		  bootbox.bconfirm("确定是否下载？", function (re) {
                  if (re) {
                  	 var url="/qrcode/download?id="+id+"&qcname="+qcname+"&isAutoDraw=2";
                       window.open(url);
                       window.location.href=window.location.href; 
               		   window.location.reload; 
                  }
               });
    	  }else{
    		  if(type==1){
        		  //盒码和瓶码逻辑
        		  $('#md_btn_down').modal('show');
        		  window.form_edit_down.id.value=$(this).data("id");
        		  window.form_edit_down.qcname.value=$(this).data("qcname");
        	  }else{
        		//箱码逻辑：直接下载即可，不做修改，箱码仅用于物流码
        		  var id = $(this).data("id");
                  var qcname = $(this).data("qcname");
                  bootbox.bconfirm("确定是否下载？", function (re) {
                    if (re) {
                    	 var url="/qrcode/download?id="+id+"&qcname="+qcname+"&isAutoDraw=3";
                         window.open(url);
                         window.location.href=window.location.href; 
                 		 window.location.reload; 
                    }
                 });
        	  } 
    	  }
    });
    $("[name='btn_save_edit_down']").click(function () {
        var form = window.form_edit_down;
        var id = form.id.value;
        var qcname = form.qcname.value;
        var isAutoDraw = form.isAutoDraw.value;
        var url="/qrcode/download?id="+id+"&qcname="+qcname+"&isAutoDraw="+isAutoDraw;
        window.open(url);
        form.reset();
        window.location.href=window.location.href; 
  	  window.location.reload; 
      });
    
    //短地址下载
    $("[name='btn_shortdown']").click(function () {
    	  var type=$(this).data("type");
    	  var history=$(this).data("history");
    	  if(history==0){
    		  var id = $(this).data("id");
              var qcname = $(this).data("qcname");
    		  bootbox.bconfirm("确定是否下载？", function (re) {
                  if (re) {
                  	 var url="/qrshortcode/download?id="+id+"&qcname="+qcname+"&isAutoDraw=2";
                       window.open(url);
                       window.location.href=window.location.href; 
               		   window.location.reload; 
                  }
               });
    	  }else{
    		  var id = $(this).data("id");
              var qcname = $(this).data("qcname");
    		  if(type==1){
        		  //盒码和瓶码逻辑
//        		  $('#md_shortbtn_down').modal('show');
//        		  window.form_shortedit_down.id.value=$(this).data("id");
//        		  window.form_shortedit_down.qcname.value=$(this).data("qcname");
    			  bootbox.bconfirm("确定是否下载？", function (re) {
                      if (re) {
                      	 var url="/qrshortcode/download?id="+id+"&qcname="+qcname+"&isAutoDraw=4";
                           window.open(url);
                           window.location.href=window.location.href; 
                   		 window.location.reload; 
                      }
                   });
        	  }else{
        		//箱码逻辑：直接下载即可，不做修改，箱码仅用于物流码
                  bootbox.bconfirm("确定是否下载？", function (re) {
                    if (re) {
                    	 var url="/qrshortcode/download?id="+id+"&qcname="+qcname+"&isAutoDraw=3";
                         window.open(url);
                         window.location.href=window.location.href; 
                 		 window.location.reload; 
                    }
                 });
        	  } 
    	  }
    });
    
    $("[name='btn_shortsave_edit_down']").click(function () {
        var form = window.form_shortedit_down;
        var id = form.id.value;
        var qcname = form.qcname.value;
        var isAutoDraw = form.isAutoDraw.value;
        var url="/qrshortcode/download?id="+id+"&qcname="+qcname+"&isAutoDraw="+isAutoDraw;
        window.open(url);
        form.reset();
        window.location.href=window.location.href; 
  	  	window.location.reload; 
      });

    
    

    <!-- ---------------  图片下载 开始------------------   -->
    $(".btn_down_image").click(function () {
        var type=$(this).data("type");
        var history=$(this).data("history");
        console.log(history);
        if(history==0){
            var id = $(this).data("id");
            var qcname = $(this).data("qcname");
            bootbox.bconfirm("确定是否下载？", function (re) {
                if (re) {
                    var url="/download/qrcodeImage?id="+id+"&qcname="+qcname+"&isAutoDraw=2";
                    window.open(url);
                }
            });
        }else{
            if(type==1){
                //盒码和瓶码逻辑
                $('#md_btn_down_image').modal('show');
                window.form_edit_down_image.id.value=$(this).data("id");
                window.form_edit_down_image.qcname.value=$(this).data("qcname");
            }else{
                //箱码逻辑：直接下载即可，不做修改，箱码仅用于物流码
                var id = $(this).data("id");
                var qcname = $(this).data("qcname");
                bootbox.bconfirm("确定是否下载？", function (re) {
                    if (re) {
                        var url="/download/qrcodeImage?id="+id+"&qcname="+qcname+"&isAutoDraw=3";
                        window.open(url);
                    }
                });
            }
        }
    });

    $("[name='btn_save_edit_down_image']").click(function () {
        var form = window.form_edit_down_image;
        var id = form.id.value;
        var qcname = form.qcname.value;
        var isAutoDraw = form.isAutoDraw.value;
        var url="/download/qrcodeImage?id="+id+"&qcname="+qcname+"&isAutoDraw="+isAutoDraw;
        window.open(url);
        form.reset();
    });

    <!-- ---------------  图片下载 结束------------------   -->
    
    //数据导出
    $("[name='btn_export']").click(function () {
        var qcname = $("[name='qcname']").val();
        var state = $("[name='state']").val();
        var isprint = $("[name='is_print']").val();
        var tsno = $("[name='tsno']").val();
        var teno = $("[name='teno']").val();
        var ysno = $("[name='ysno']").val();
        var yeno = $("[name='yeno']").val();
        
        //有数据时才能导出
        if($("#resultTotal_hidden").val() != 0){
        	bootbox.bconfirm("确定是否导出？", function (re) {
                if (re) {
                	 var url="/qrcode/export?qcname="+qcname+"&state="+state+"&isprint="+isprint+"&tsno="+tsno
                	 +"&teno="+teno+"&ysno="+ysno+"&yeno="+yeno;
                     window.open(url);
                }
            });
        }else{
        	bootbox.balert("没有数据，无法导出!");
        }
    });

});
